package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Attribute;
import net.zjitc.mapper.AttributeMapper;
import net.zjitc.service.AttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 毛钰深
 * @Create 2021/12/17 11:33
 */
@Service
@Transactional
public class IAttributeService implements AttributeService {

    @Autowired
    private AttributeMapper attributeMapper;


    @Override
    public ResponseResult<List<Attribute>> findAttributeByCatIdAndSel(Integer id, String sel) {
        ResponseResult<List<Attribute>> responseResult=new ResponseResult<>();
        List<Attribute> attributes = attributeMapper.selectList(Wrappers.lambdaQuery(Attribute.class).eq(Attribute::getCat_id,id).eq(Attribute::getAttr_sel,sel));
        if (attributes!=null){
            responseResult.setSuccess(attributes);
        }else{
            responseResult.setNotFound();
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Attribute> addAttribute(Integer id, Attribute attribute) {
        ResponseResult<Attribute> responseResult=new ResponseResult<>();
        attribute.setCat_id(id);
        attribute.setAttr_vals(attribute.getAttr_vals()==null?"":attribute.getAttr_vals());
        int attr=attributeMapper.insert(attribute);
        if (attr!=0){
            responseResult.setCreated("添加成功");
        }else{
            responseResult.setNotFound("添加失败");
        }
        responseResult.setData(attribute);
        return responseResult;
    }

    @Override
    public ResponseResult<Attribute> deleteAttribute(Integer id, Integer attrId) {
        ResponseResult<Attribute> responseResult=new ResponseResult<>();
        int delete = attributeMapper.delete(Wrappers.lambdaUpdate(Attribute.class).eq(Attribute::getAttr_id, attrId).eq(Attribute::getCat_id, id));
        if (delete!=0){
            responseResult.setSuccess("删除成功");
        }else{
            responseResult.setNotFound("删除失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Attribute> selectById(Integer id, Integer attrId) {
        ResponseResult<Attribute> responseResult=new ResponseResult<>();
        Attribute attribute = attributeMapper.selectOne(Wrappers.lambdaQuery(Attribute.class).eq(Attribute::getCat_id, id).eq(Attribute::getAttr_id, attrId));
        if (attribute!=null){
            responseResult.setSuccess("查询成功",attribute);
        }else{
            responseResult.setNotFound("查询失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Attribute> updateAttribute(Integer id, Integer attrId, Attribute attribute) {
        ResponseResult<Attribute> responseResult=new ResponseResult<>();
        attribute.setCat_id(id);
        attribute.setAttr_id(attrId);
        int update = attributeMapper.update(attribute, Wrappers.lambdaQuery(Attribute.class).eq(Attribute::getCat_id, id).eq(Attribute::getAttr_id, attrId));
        if (update!=0){
            responseResult.setSuccess("修改成功",attribute);
        }else{
            responseResult.setNotFound("查询失败");
        }
        return responseResult;
    }


}
